home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / osrc.arc / IPCMD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-05  |  4.9 KB  |  233 lines

  1. /* IP-related user commands */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "internet.h"
  6. #include "timer.h"
  7. #include "netuser.h"
  8. #include "iface.h"
  9. #include "ip.h"
  10. #include "cmdparse.h"
  11.  
  12. extern struct ip_stats Ip_stats;
  13. extern struct reasm *Reasmq;
  14. extern char Badhost[];
  15. int doipaddr(),doipstat(),dottl();
  16. char *inet_ntoa();
  17.  
  18. struct cmds Ipcmds[] = {
  19.     "address",    doipaddr,    0,    0, NULLCHAR,
  20.     "status",    doipstat,    0,    0, NULLCHAR,
  21.     "ttl",        dottl,        0,    0, NULLCHAR,
  22.     NULLCHAR,    NULLFP,        0,    0,
  23.         "ip subcommands: address status ttl",
  24. };
  25. doip(argc,argv,envp)
  26. int argc;
  27. char *argv[];
  28. void *envp;
  29. {
  30.     return subcmd(Ipcmds,argc,argv,envp);
  31. }
  32. int
  33. doipaddr(argc,argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     int32 n;
  38.  
  39.     if(argc < 2) {
  40.         printf("%s\n",inet_ntoa(Ip_addr));
  41.     } else if((n = resolve(argv[1])) == 0){
  42.         printf(Badhost,argv[1]);
  43.         return 1;
  44.     } else
  45.         Ip_addr = n;
  46.     return 0;
  47. }
  48. int
  49. dottl(argc,argv)
  50. char *argv[];
  51. {
  52.     if(argc < 2)
  53.         printf("%u\n",uchar(Ip_ttl));
  54.     else
  55.         Ip_ttl = atoi(argv[1]);
  56.     return 0;
  57. }
  58.  
  59. /* "route" subcommands */
  60. int doadd(),dodrop();
  61. static struct cmds Rtcmds[] = {
  62.     "add", doadd, 0, 3,
  63.     "route add <dest addr>[/<bits>] <if name> [gateway]",
  64.  
  65.     "drop", dodrop, 0, 2,
  66.     "route drop <dest addr>[/<bits>]",
  67.  
  68.     NULLCHAR, NULLFP, 0, 0,
  69.     "route subcommands: add, drop",
  70. };
  71.  
  72. /* Display and/or manipulate routing table */
  73. int
  74. doroute(argc,argv,envp)
  75. int argc;
  76. char *argv[];
  77. void *envp;
  78. {
  79.     if(argc < 2){
  80.         dumproute();
  81.         return 0;
  82.     }
  83.     return subcmd(Rtcmds,argc,argv,envp);
  84. }
  85. /* Add an entry to the routing table
  86.  * E.g., "add 1.2.3.4 ax0 5.6.7.8 3"
  87.  */
  88. int
  89. doadd(argc,argv)
  90. int argc;
  91. char *argv[];
  92. {
  93.     struct iface *ifp;
  94.     int32 dest,gateway;
  95.     unsigned bits;
  96.     char *bitp;
  97.  
  98.     if(strcmp(argv[1],"default") == 0){
  99.         dest = 0;
  100.         bits = 0;
  101.     } else {
  102.         if((dest = resolve(argv[1])) == 0){
  103.             printf(Badhost,argv[1]);
  104.             return 1;
  105.         }
  106.  
  107.         /* If IP address is followed by an optional slash and
  108.          * a length field, (e.g., 128.96/16) get it;
  109.          * otherwise assume a full 32-bit address
  110.          */
  111.         if((bitp = strchr(argv[1],'/')) != NULLCHAR){
  112.             bitp++;
  113.             bits = atoi(bitp);
  114.         } else
  115.             bits = 32;
  116.     }
  117.     for(ifp=Ifaces;ifp != NULLIF;ifp = ifp->next){
  118.         if(strcmp(argv[2],ifp->name) == 0)
  119.             break;
  120.     }
  121.     if(ifp == NULLIF){
  122.         printf("Interface \"%s\" unknown\n",argv[2]);
  123.         return 1;
  124.     }
  125.     if(argc > 3){
  126.         if((gateway = resolve(argv[3])) == 0){
  127.             printf(Badhost,argv[3]);
  128.             return 1;
  129.         }
  130.     } else {
  131.         gateway = 0;
  132.     }
  133.     rt_add(dest,bits,gateway,ifp);
  134.     return 0;
  135. }
  136. /* Drop an entry from the routing table
  137.  * E.g., "drop 128.96/16
  138.  */
  139. int
  140. dodrop(argc,argv)
  141. int argc;
  142. char *argv[];
  143. {
  144.     char *bitp;
  145.     unsigned bits;
  146.     int32 n;
  147.  
  148.     if(strcmp(argv[1],"default") == 0){
  149.         n = 0;
  150.         bits = 0;
  151.     } else {
  152.         /* If IP address is followed by an optional slash and length field,
  153.          * (e.g., 128.96/16) get it; otherwise assume a full 32-bit address
  154.          */
  155.         if((bitp = strchr(argv[1],'/')) != NULLCHAR){
  156.             bitp++;
  157.             bits = atoi(bitp);
  158.         } else
  159.             bits = 32;
  160.  
  161.         if((n = resolve(argv[1])) == 0){
  162.             printf(Badhost,argv[1]);
  163.             return 1;
  164.         }
  165.     }
  166.     return rt_drop(n,bits);
  167. }
  168.  
  169. /* Dump IP routing table
  170.  * Dest              Length    Interface    Gateway          Use
  171.  * 192.001.002.003   32        sl0          192.002.003.004
  172.  */
  173. int
  174. dumproute()
  175. {
  176.     register unsigned int i,bits;
  177.     register struct route *rp;
  178.  
  179.     printf("Dest              Length    Interface    Gateway          Use\n");
  180.     if(R_default.iface != NULLIF){
  181.         printf("default           0         %-13s",
  182.          R_default.iface->name);
  183.         if(R_default.gateway != 0)
  184.             printf("%-17s",inet_ntoa(R_default.gateway));
  185.         else
  186.             printf("%-17s","");
  187.         printf("%lu\n",R_default.uses);
  188.     }
  189.     for(bits=1;bits<=32;bits++){
  190.         for(i=0;i<NROUTE;i++){
  191.             for(rp = Routes[bits-1][i];rp != NULLROUTE;rp = rp->next){
  192.                 printf("%-18s",inet_ntoa(rp->target));
  193.                 printf("%-10u",bits);
  194.                 printf("%-13s",rp->iface->name);
  195.                 if(rp->gateway != 0)
  196.                     printf("%-17s",inet_ntoa(rp->gateway));
  197.                 else
  198.                     printf("%-17s","");
  199.                 printf("%lu\n",rp->uses);
  200.             }
  201.         }
  202.     }
  203.     return 0;
  204. }
  205.  
  206. int
  207. doipstat(argc,argv)
  208. int argc;
  209. char *argv[];
  210. {
  211.     register struct reasm *rp;
  212.     register struct frag *fp;
  213.  
  214.     printf("IP: total %ld runt %u len err %u vers err %u",
  215.         Ip_stats.total,Ip_stats.runt,Ip_stats.length,Ip_stats.version);
  216.     printf(" chksum err %u badproto %u\n",
  217.         Ip_stats.checksum,Ip_stats.badproto);
  218.  
  219.     if(Reasmq != NULLREASM)
  220.         printf("Reassembly fragments:\n");
  221.     for(rp = Reasmq;rp != NULLREASM;rp = rp->next){
  222.         printf("src %s",inet_ntoa(rp->source));
  223.         printf(" dest %s",inet_ntoa(rp->dest));
  224.         printf(" id %u pctl %u time %lu len %u\n",
  225.             rp->id,uchar(rp->protocol),read_timer(&rp->timer),rp->length);
  226.         for(fp = rp->fraglist;fp != NULLFRAG;fp = fp->next){
  227.             printf(" offset %u last %u\n",fp->offset,fp->last);
  228.         }
  229.     }
  230.     doicmpstat();
  231.     return 0;
  232. }
  233.